package com.sinodata.bsm.center.web.action.event;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.NotifyHisBean;
import com.sinodata.bsm.center.purview.FunctionConstants;
import com.sinodata.bsm.center.purview.UserFunctionValidate;
import com.sinodata.bsm.center.service.notify.NotifyHisService;
import com.sinodata.bsm.center.user.UserUtil;
import com.sinodata.bsm.center.web.action.BaseExportAction;
import com.sinodata.bsm.common.constants.EventConstants;
import com.sinodata.bsm.common.constants.NotifyConstants;
import com.sinodata.bsm.common.vo.Notify;

/**
 * <p>
 * Description: 通知历史导出
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-6 PM 4:58:48      liulibo        1.0         To create
 * </p>
 *
 * @since   
 * @see     
 */
public class NotifyExportAction extends BaseExportAction<Notify> {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -6665679220620259474L;

    @Autowired
    private NotifyHisService notifyHisService;

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseExportAction#doExport()
     */
    @Override
    public String doExport() throws Exception {
        String fileName = "通知查询";
        String displayFileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
        response.setContentType("APPLICATION/msexcel; charset=utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=\"" + displayFileName.trim().replaceAll("\\s", "") + ".xls\"");
        HSSFSheet sheet = workBook.createSheet("通知查询");
        //设置列的宽度  
        sheet.setColumnWidth(0, 4000);
        sheet.setColumnWidth(1, 4000);
        sheet.setColumnWidth(2, 5000);
        sheet.setColumnWidth(3, 8000);
        sheet.setColumnWidth(4, 20000);
        sheet.setColumnWidth(5, 4000);
        sheet.setColumnWidth(6, 5000);
        sheet.setColumnWidth(7, 4000);
        //表头
        HSSFCellStyle style = getStyle(STYLE_HEADING);
        HSSFRow row = sheet.createRow(0);
        HSSFCell userHead = row.createCell(0);
        userHead.setCellValue(new HSSFRichTextString("通知人"));
        userHead.setCellStyle(style);
        HSSFCell modeHead = row.createCell(1);
        modeHead.setCellValue(new HSSFRichTextString("通知方式"));
        modeHead.setCellStyle(style);
        HSSFCell timeHead = row.createCell(2);
        timeHead.setCellValue(new HSSFRichTextString("通知时间"));
        timeHead.setCellStyle(style);
        HSSFCell resNameHead = row.createCell(3);
        resNameHead.setCellValue(new HSSFRichTextString("资源名称"));
        resNameHead.setCellStyle(style);
        HSSFCell msgHead = row.createCell(4);
        msgHead.setCellValue(new HSSFRichTextString("通知内容"));
        msgHead.setCellStyle(style);
        HSSFCell eventLevelHead = row.createCell(5);
        eventLevelHead.setCellValue(new HSSFRichTextString("事件级别"));
        eventLevelHead.setCellStyle(style);
        HSSFCell eventTimeHead = row.createCell(6);
        eventTimeHead.setCellValue(new HSSFRichTextString("事件产生时间"));
        eventTimeHead.setCellStyle(style);
        HSSFCell eventStatusHead = row.createCell(7);
        eventStatusHead.setCellValue(new HSSFRichTextString("事件状态"));
        eventStatusHead.setCellStyle(style);

        //查询条件
        String dimension = request.getParameter("dimension");
        String resIdSearchScope = request.getParameter("resId");
        String resIdAllScope = (resIdSearchScope == null || resIdSearchScope.equals("")) ? "" : resIdSearchScope;
        String level = request.getParameter("level");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");
        String eventTypeId = request.getParameter("eventTypeId");
        String mode = request.getParameter("mode");
        String userId = request.getParameter("userId");
        Condition condition = new Condition();
        Map<String, String> map = new HashMap<String, String>();
        //当前用户可见资源范围
        List<Long> authScopeList = UserFunctionValidate.getResIdListForUser(FunctionConstants.VIEW_RES);
        StringBuffer resIds = new StringBuffer();
        if (authScopeList != null && authScopeList.size() != 0) {
            for (Long resId : authScopeList) {
                resIds.append(resId).append(",");
            }
            resIds.delete(resIds.lastIndexOf(","), resIds.length());
        }
        if (resIds.toString() != null && !resIds.toString().equals("")) {
            String currentUserId = UserUtil.getInstance().getCurrentUserId();
            if (!UserUtil.getInstance().isSysAdmin(currentUserId) && !UserUtil.getInstance().isGuest(currentUserId)) {//guest用户有全部数据的查看权限
                if (resIdAllScope.equals("")) {
                    resIdAllScope = resIds.toString();
                } else {
                    resIdAllScope = resIdAllScope + "," + resIds.toString();
                }
            }
        } else {
            workBook.write(response.getOutputStream());
            return null;
        }
        if (level != null && !level.equals("")) {
            map.put("level_", level);
        } else {
            map.put("level_", "-1");
        }
        if (eventTypeId != null && !eventTypeId.equals("-1")) {
            map.put("res_type_id", eventTypeId);
        }
        if (beginDate != null && !beginDate.equals("")) {
            map.put("beginDate", beginDate);
        }
        if (endDate != null && !endDate.equals("")) {
            map.put("endDate", endDate);
        }
        if (mode != null && !mode.equals("")) {
            map.put("notify_mode", mode);
        } else {
            map.put("notify_mode", "-1");
        }
        if (userId != null && !userId.equals("")) {
            map.put("userId", userId);
        }
        //资源ID条件
        if (!resIdAllScope.equals("")) {
            map.put("resIds", resIdAllScope);
        }
        condition.setWhere(map);
        List<NotifyHisBean> list = notifyHisService.findByCondition(condition, dimension);
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simple = new SimpleDateFormat(format);
        //表体赋值
        int rowNum = 1;
        style = getStyle(STYLE_CONTENT);
        for (NotifyHisBean notifyHisBean : list) {
            HSSFRow rowBody = sheet.createRow(rowNum++);
            HSSFCell user = rowBody.createCell(0);
            user.setCellValue(new HSSFRichTextString((UserUtil.getInstance().getUserById(notifyHisBean.getNotifyHis().getUser()) == null ? notifyHisBean.getNotifyHis().getUser() + "(已删除)" : UserUtil.getInstance().getUserById(notifyHisBean.getNotifyHis().getUser()).getFullName())));
            HSSFCell modeBody = rowBody.createCell(1);
            modeBody.setCellValue(new HSSFRichTextString(NotifyConstants.getModeDesc(notifyHisBean.getNotifyHis().getNotifyMode())));
            HSSFCell time = rowBody.createCell(2);
            time.setCellValue(new HSSFRichTextString(simple.format(notifyHisBean.getNotifyHis().getTime())));
            HSSFCell resName = rowBody.createCell(3);
            resName.setCellValue(new HSSFRichTextString(notifyHisBean.getResName()));
            HSSFCell msg = rowBody.createCell(4);
            msg.setCellValue(new HSSFRichTextString(notifyHisBean.getNotifyHis().getMsg()));
            HSSFCell eventLevel = rowBody.createCell(5);
            eventLevel.setCellValue(new HSSFRichTextString(EventConstants.getLevelDesc(notifyHisBean.getEventLevel())));
            HSSFCell eventTime = rowBody.createCell(6);
            eventTime.setCellValue(new HSSFRichTextString(simple.format(notifyHisBean.getEventOccurTime())));
            HSSFCell eventStatus = rowBody.createCell(7);
            eventStatus.setCellValue(new HSSFRichTextString(EventConstants.getStatusDesc(notifyHisBean.getEventStatus())));
            if (rowNum % 2 != 0) {
                user.setCellStyle(style);
                modeBody.setCellStyle(style);
                time.setCellStyle(style);
                resName.setCellStyle(style);
                msg.setCellStyle(style);
                eventLevel.setCellStyle(style);
                eventTime.setCellStyle(style);
                eventStatus.setCellStyle(style);
            }
        }
        workBook.write(response.getOutputStream());
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#input()
     */
    @Override
    public String input() throws Exception {
        return null;
    }

}